import 'package:flutter/material.dart';

import '../app_options/colors.dart';
import '../enums/message_type.dart';

void showSnackBar(BuildContext context, String message,
    {MessageType messageType = MessageType.information,
    SnackBarAction? action,
    int? durationSeconds}) {
  Color foreColor = messageColor;
  Color bgColor = Colors.black;
  Size screenSize = MediaQuery.of(context).size;

  switch (messageType) {
    case MessageType.information:
    case MessageType.question:
      bgColor = messageColor;
      foreColor = Colors.black;
      break;

    case MessageType.error:
      bgColor = errorColor;
      foreColor = Colors.white;
      break;
    case MessageType.warning:
      bgColor = warningColor;
      foreColor = Colors.white;
      break;
    case MessageType.success:
      bgColor = successColor;
      foreColor = Colors.white;
      break;
  }
  double width = 320;
  if (screenSize.width >= 1024) {
    width = screenSize.width / 3;
  } else if (screenSize.width <= 600) {
    width = screenSize.width - 20;
  }

  String msg = message;
  if (msg.length > 200) {
    msg = "${msg.substring(0, 200)}......";
  }

  ScaffoldMessenger.of(context).showSnackBar(
    SnackBar(
      action: action,
      backgroundColor: bgColor,
      width: width,
      duration: durationSeconds != null
          ? Duration(seconds: durationSeconds)
          : const Duration(seconds: 6),
      behavior: SnackBarBehavior.floating,
      showCloseIcon: true,
      content: Text(msg, style: TextStyle(color: foreColor)),
    ),
  );
}
